package com.github.zzay.care.controller;

import com.github.zzay.care.dto.EventDto;
import com.github.zzay.care.entity.Event;
import com.github.zzay.care.service.EventService;
import com.github.zzay.common.core.model.ResponseBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author zzay
 * @className EventController
 * @description Controller class for entity "Event"
 * @create 2022/07/08 10:49
 */
@Slf4j
@RestController
@RequestMapping("/event")
@CrossOrigin
@Tag(name = "EventController", description = "事件信息操作相关接口")
public class EventController {

    /**
     * Event Service.
     */
    @Autowired
    private EventService eventService;

    /**
     * Add an event record into the system.
     *
     * @param eventDto Event DTO
     * @return Operation Result
     */
    @Operation(summary = "添加事件记录", description = "添加事件记录：",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加事件成功"),
            @ApiResponse(responseCode = "400", description = "添加事件失败")
    })
    @PostMapping("/add")
    public ResponseBean<Map<String, Object>> addEvent(
            @Parameter(name = "event") @RequestBody EventDto eventDto) {
        Map<String, Object> data = new HashMap<>();
        Event event = new Event();
        event.setEventType(eventDto.getEventType());
        event.setEventLocation(eventDto.getEventLocation());
        event.setEventDate(eventDto.getEventDate());
        event.setEventDesc(eventDto.getEventDesc());
        event.setEventImage(eventDto.getEventImage());
        event.setElderlyPersonId(eventDto.getElderlyPersonId());
        Boolean success = eventService.save(event);
        data.put("success", success);
        if (success) {
            log.info("======== AddEvent - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully add an event", data);
        }
        log.info("======== AddEvent - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to add an event", data);
    }

    /**
     * Get all event records.
     *
     * @return Operation Result
     */
    @Operation(summary = "查询所有事件", description = "查询所有事件：查询所有事件",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询事件成功"),
            @ApiResponse(responseCode = "400", description = "查询事件失败")
    })
    @GetMapping("/getAll")
    public ResponseBean<Map<String, Object>> getAllEvents() {
        Map<String, Object> data = new HashMap<>();
        List<EventDto> eventList = eventService.getAll();
        if (eventList == null) {
            // failed to get information
            data.put("success", false);
            log.info("======== GetAllEvents - Fail ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get information successfully
            data.put("success", true);
            data.put("info", eventList);
            log.info("======== GetAllEvents - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

}
